<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>
    <?php
      if(is_array($_SESSION['list_search_data'][0])){
          echo $_SESSION['my_kuhu']['kehu_old']."岁".$_SESSION['list_search_data'][0][1];
      }
      else{
           echo $_SESSION['my_kuhu']['kehu_old']."岁".$_SESSION['list_search_data'][0];
    }
    ?>
  </title>
  <!-- <title>搜索结果</title> -->
  <script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/jquery"></script>
  <link rel="stylesheet" href="__PUBLIC__/script/jquery-weui/dist/lib/weui.min.css?v=1.0">
  <link rel="stylesheet" href="__PUBLIC__/script/jquery-weui/dist/css/jquery-weui.min.css?v=1.0">
  <link rel="stylesheet" href="__PUBLIC__/styles/base.css?v=1.0">
  <link rel="stylesheet" href="__PUBLIC__/styles/new_pingu.css?v=1.0">
  <link rel="stylesheet" href="__PUBLIC__/styles/last_pingu.css?v=1.0">
  <link rel="stylesheet" href="__PUBLIC__/styles/last_product_flex.css?v=1.0">
  <!-- 产品详细 -->
  <link rel="stylesheet" href="__PUBLIC__/styles/pro_detail_page.css?v=1.0">
</head>

<!--
  这一页写不写ajax……妈的简直天大难题
 -->
<body>
  <div style="display:none;">
    <!-- 查看产品详情 -->
    <input type="hidden" name="产品详情" id='pro_detail_page' value="{:U('WebUser/pro_detail_page')}">
  </div>
  <div class="fixed_ele header">
    <?php
      if(is_array($_SESSION['list_search_data'][0])){
          echo $_SESSION['my_kuhu']['kehu_old']."岁".$_SESSION['list_search_data'][0][1];
      }
      else{
           echo $_SESSION['my_kuhu']['kehu_old']."岁".$_SESSION['list_search_data'][0];
    }
    ?>
  </div>
  <!-- 容器 -->
  <div class="weui-tab js-last_nav">
    <div class="weui-navbar">
      <a class="weui-navbar__item weui-bar__item--on" href="#tab1">
        <img src="__PUBLIC__/res/u1946.png" alt="u1946">
        <br>
        搜索结果
      </a>
      <a class="weui-navbar__item" href="#tab2" id='js-data_panel'>
        <img src="__PUBLIC__/res/u1936.png" alt="u1936">
        <br>
        客户资料
      </a>
      <if condition='$Think.session.my_kuhu.data neq null'>
        <a class="weui-navbar__item" href="#tab3">
          <img src="__PUBLIC__/res/u1941.png" alt="u1941">
          <br>
          渠道留言
        </a>
      <else/>
        <a class="weui-navbar__item" onclick="alert('请先添加客户资料');jumpTodataPage()">
          <img src="__PUBLIC__/res/u1941.png" alt="u1941">
          <br>
          渠道留言
        </a>
      </if>
    </div>
    <div class="weui-tab__bd js-iframe">
      <div id="tab1" class="weui-tab__bd-item weui-tab__bd-item--active">
        <iframe src="{:U('WebUser/panel_search')}" frameborder="0" width="100%"></iframe>
      </div>
      <div id="tab2" class="weui-tab__bd-item">
          <iframe src="{:U('WebUser/panel_p_data')}" frameborder="0" width="100%"></iframe>
      </div>
      <div id="tab3" class="weui-tab__bd-item">
          <iframe src="{:U('Chat/panel_chat')}" frameborder="0" width="100%" id='chatpage'></iframe>
      </div>
    </div>
  </div>
  <div id="fileupload" class="weui-popup__container">
    <div class="weui-popup__overlay"></div>
    <div class="weui-popup__modal">
      你的内容放在这里...
    </div>
  </div>
  <div id="prodetail" class="weui-popup__container">
    <div class="weui-popup__overlay"></div>
    <div class="weui-popup__modal" style='padding-top: 1rem;'>
      网络错误！无法获取详情页面
    </div>
  </div>

  <script src="__PUBLIC__/script/common.js" common-data-1="{:U('Mix/dest')}"></script>
  <script src="__PUBLIC__/script/jquery-weui/dist/js/jquery-weui.min.js"></script>
  <!-- 接受表单数据并提交 -->
  <script>
    window.addEventListener('message', fromNav, false)
    function fromNav(event) {
      if (event.data.formData && event.data.formData.length > 1) {
        $('body').append(event.data.formData)
        // 提交前留下一个标志方便搜索页面生成标签
        sessionStorage.setItem('currentFilter', JSON.stringify($('#tempGenForm').serializeArray()))
        $('#tempGenForm').submit()
      } else if (event.data.refresh === 'ok') {
        location.reload(true)
      } else if (event.data.searchCount) {
        // 安卓一直都可以这样修改title，17年3月后的ios版本也可以，开发者工具zz一直不行
        document.title = `共${event.data.searchCount}条结果`
      } else if (event.data.jumpTo && event.data.jumpTo === 'data') {
        $(`.weui-navbar__item[href='#tab2']`).click()
      } else if (event.data.jumpTo && event.data.jumpTo === 'chat') {
        $(`.weui-navbar__item[href='#tab3']`).click()
        $('#tab3 iframe')[0].contentWindow.postMessage({clickPerson: 'true'}, '*')
      } else if (event.data.jumpTo && event.data.jumpTo === 'true') {
        // 跳回添加渠道的页面，就是搜索结果了
        $(`.weui-navbar__item[href='#tab1']`).click()
        // 发送消息提示点击
        $('#tab1 iframe')[0].contentWindow.postMessage({clickPerson: 'true'}, '*')
        debugger
      } else if (event.data.name && event.data.name === 'popup_file') {
        $(`#fileupload`).popup()
      } else if (event.data.popup_pro_detail && event.data.pro_list && event.data.pro_list_index) {
        window.__pro__fetching = null
        const res = event.data.popup_pro_detail
        const res_list = window.__pro__list = event.data.pro_list
        const res_index = window.__pro__index = event.data.pro_list_index - 2
        $(`#prodetail .weui-popup__modal`).empty()
        $(`#prodetail .weui-popup__modal`).append(res)
        // 处理详细页面的一些bug并在处理过后popup
        dealWithProDetail()
        $(`#prodetail`).popup()
        $('.js-last_nav').css('opacity', '0')
      }
    }
    /* 获取详细资料页面 */
    function fetchIndexPro (evt) {
      evt.preventDefault()
      evt.stopPropagation()
      if (window.__pro__fetching === null && (window.__pro__index || window.__pro__index === 0) && window.__pro__list && evt.data && evt.data.type === 'pre') {
        if (window.__pro__index === 0) {
          $.toptip('已经是第一个产品了', 'success')
          return
        } else {
          window.__pro__index -= 1
          window.__pro__fetching = window.__pro__list[window.__pro__index]
        }
      } else if (window.__pro__fetching === null && (window.__pro__index || window.__pro__index === 0) && window.__pro__list && evt.data && evt.data.type === 'next') {
        if (window.__pro__index === window.__pro__list.length - 1) {
          $.toptip('已经是最后一个产品了', 'success')
          return
        } else {
          window.__pro__index += 1
          window.__pro__fetching = window.__pro__list[window.__pro__index]
        }
      } else if (window.__pro__fetching !== null || (!window.__pro__index && window.__pro__index !== 0) || !window.__pro__list || !evt.data || !evt.data.type) {
        return
      } else {
        return
      }
      // 获取产品详细资料
      const ajaxURL = String($(`#pro_detail_page`).val())
      const ajaxData = {
        g_id: window.__pro__fetching
      }
      $.ajax(ajaxURL, {
        method: 'post',
        data: ajaxData,
        cache: false,
        beforeSend: () => {
          $.toptip(`正在获取详情`, 'success')
        }
      }).fail((err) => {
        window.__pro__fetching = null
        $.closePopup()
        $('.js-last_nav').css('opacity', '1')
        $.toptip(`详情页面获取时发生网络错误，请检查网络`, 'error')
        throw err
      }).done((res) => {
        window.__pro__fetching = null
        $.toptip(`服务器已经返回,正在处理。。。`, 400, 'success')
        $(`#prodetail .weui-popup__modal`).empty()
        $(`#prodetail .weui-popup__modal`).append(res)
        // 处理详细页面的一些bug并在处理过后popup
        dealWithProDetail()
        return
      })
    }
    /* 处理详细页面bug */
    function dealWithProDetail () {
      // 处理海报意外字符串
      if ($('#prodetail .haibao_pro_name').text() === 'undefined') {
        $('#prodetail .haibao_pro_name').text('自定义')
      }
      // 处理id
      if ($('#prodetail #pro_id').length === 1){
        let rawID = $('#prodetail #pro_id').text().replace(/\D/g, '')
        let resultID = ``
        if (sessionStorage.getItem(`pro_id${rawID}`) !== null) {
          resultID = sessionStorage.getItem(`pro_id${rawID}`)
        } else {
          const rawIDAlt = addPreZero(rawID, 5)
          resultID = `A${genRandEngCode(1)}${genRandCode(1)}${rawIDAlt.substr(0, 4)}${genRandCode(2)}${rawIDAlt.substr(-1)}`
          sessionStorage.setItem(`pro_id${rawID}`, resultID)
        }
      } else {
        // 按揭员处理
        $('#prodetail #title_only_anjie_can_see_me').prev('span').remove()
        $('#prodetail #title_only_anjie_can_see_me').remove()
      }
      // 处理征信
      if ($(`#prodetail .dataList .smallItem:eq(0)`).attr('inori-data') !== undefined) {
        const textArray = $(`#prodetail .dataList .smallItem:eq(0)`).attr('inori-data').split(',')
        if (textArray > 0) {
          let templ = ``
          $.each(textArray, (index, item) => {
            templ += `<span>${item}</span>`
          })
          $(`#prodetail .zhengxinOnly`).next('.dataItem').find('.value').append(templ)
        }
      }
      // 处理禁入行业
      if ($(`#prodetail #pro_detail_tab3 .jinzhi_tag`).length === 1) {
        const textArray = $(`#prodetail #pro_detail_tab3 .jinzhi_tag`).text().split(',')
        let textEle = ``
        $.each(textArray, (index, item) => {
          textEle += `<div class='jinzhi_tag'>${item}</div>`
        })
        $(`#prodetail #pro_detail_tab3 .yiban.jinzhi`).empty()
        $(`#prodetail #pro_detail_tab3 .yiban.jinzhi`).append(textEle)
      }
      // 处理利息
      $.each($('.lixiItem'), (index, item) => {
        if($(item).attr('item-id') === '0') {
          $(item).remove()
        }
      })
      // 处理前后翻页
      if ($(`#prodetail .big_icon.back`).length === 1 && $(`#prodetail .big_icon.next`).length === 1) {
        $(`#prodetail .big_icon.back`).off('click')
        $(`#prodetail .big_icon.next`).off('click')
        $(`#prodetail .big_icon.back`).on({click: fetchIndexPro}, {type: 'pre'})
        $(`#prodetail .big_icon.next`).on({click: fetchIndexPro}, {type: 'next'})
      }
    }
    /* 跳转到资料填写 */
    function jumpTodataPage () {
      if ($('#js-data_panel').length === 1) {
        $('#js-data_panel').click()
      } else {
        // alert(`请点击客户资料页面`)
      }
    }
    $(document).ready(() => {
      // 跳转标志在提交客户资料时打下，一次有效，所以需要清除
      const ifChat = sessionStorage.getItem('jumpToRoom') === 'true'
      if (ifChat) {
        sessionStorage.setItem('jumpToRoom', 'false')
        $(`.weui-navbar__item[href='#tab3']`).click()
      } else if (sessionStorage.getItem('currentIframe')) {
        const targetID = sessionStorage.getItem('currentIframe')
        $(`.weui-navbar__item[href='${targetID}']`).click()
        sessionStorage.setItem('currentIframe', '')
      }

      // 侦听目前在那个标签，如果是聊天就sendmessage提示更新消息
      function fetchMention () {
        setTimeout(fetchMention, 60000)
        if ($('.weui-bar__item--on').attr('href') === '#tab3') {
          $('#chatpage')[0].contentWindow.postMessage({
            fetch: 'true'
          }, '*');
        }
      }
      // 防止页面后退
      // 页面载入时使用pushState插入一条历史记录
      // 此处不能插入随机数，后端get的时候需要判断……
      // 仅在正常查询时起作用
      function canNotBack () {
        if(!sessionStorage.getItem('kehu_old') || sessionStorage.getItem('kehu_old') === '') return
        history.pushState(null, null, "{:U('WebUser/last_product')}");
        console.log('刷新');
        window.addEventListener('popstate', function(event) {
          if (sessionStorage.getItem('last_pro_back_touch')) {
            // 判断是不是第三次
            const value = Number.parseInt(sessionStorage.getItem('last_pro_back_touch'))
            if (value >0 && value >= 2) {
              // 重复后退就弹窗提示
              const mainFlag = confirm('是否需要进行新一次的评估？')
              if (mainFlag) {
                // 正在跳转
                $.toptip('正在准备进行新一次评估，请稍候。。。', 'success')
                sessionStorage.clear()
                location.href = "{:U('Wchat/main')}" + "?front_end_not_submit=" + Math.random()
              } else {
                sessionStorage.setItem('last_pro_back_touch', 1)
              }
            } else {
              sessionStorage.setItem('last_pro_back_touch', `${value + 1}`)
            }
          } else {
            sessionStorage.setItem('last_pro_back_touch', 1)
          }
            console.log('回退');
            // 点击回退时再向历史记录插入一条，以便阻止下一次点击回退
            history.pushState(null, null, "{:U('WebUser/last_product')}");
        });
      }
      canNotBack()
      setTimeout(fetchMention, 6000)
      // 如果有数据就修改标题
      let kehu_old = null
      let value = sessionStorage.getItem('kehu_old') || null
      if (value !== undefined && value !== null && value !== '[]') {
        kehu_old = value
      }
      let kehu_type_from = sessionStorage.getItem('newPingu') || null
      let kehu_type = ''
      if (kehu_type_from !== undefined && kehu_type_from !== null && kehu_type_from !== '[]') {
        let formData = JSON.parse(kehu_type_from)
        for(let item of formData) {
          if (item.name === '0') {
            // 取到客户类型
            if (item.value === '必须工薪') kehu_type = '工薪人士'
            else if (item.value === '必须自雇') kehu_type = '自雇人士'
          }
          if (item.name === '0[1]') {
            // 取到客户类型
            kehu_type = '自由职业'
          }
        }
      }
      if (kehu_type !== '' && kehu_old !== null) {
        $('.fixed_ele.header').text(`${kehu_old}岁${kehu_type}`)
      } else if(kehu_type !== '' && kehu_old === null) {
        $('.fixed_ele.header').text(`${kehu_type}`)
      } else if(kehu_type === '' && kehu_old !== null) {
        $('.fixed_ele.header').text(`${kehu_old}岁`)
      }
    })
  </script>
  <script>
    $(document).ready(() => {
      // 禁止穿透
      $('#prodetail .weui-popup__overlay').on('touchstart', (evt) => {
        evt.preventDefault()
        $.closePopup()
        $('.js-last_nav').css('opacity', '1')
      })
      // 关闭弹层
      $('#prodetail').on('touchstart', (evt) => {
        if($(evt.target).attr('id') !== 'prodetail') {
          return
        }
        evt.preventDefault()
        $.closePopup()
        $('.js-last_nav').css('opacity', '1')
      })
      // 固定刷新表现
      $('.weui-navbar__item').on('click', (evt) => [
        sessionStorage.setItem('currentIframe', $(evt.target).closest('.weui-navbar__item').attr('href'))
      ])
    })
  </script>
</body>
</html>